<?php
	include "layout.php";
	head();
	if (!isset($_SESSION["id"])) {
		header("Location: index.php");
	}
	if (!isset($_SESSION["oid"])) {
		header("Location: base.php");
	}
?>
<h4 style="text-align: center;">Customize Order</h4>
<?php
	if (isset($_POST["submit"])) {
		extract($_POST);
		if (empty($select)) {
			echo <<<Error
				<script>
					alert("No items selected to add to inventory.");
					window.location="cust.php";
				</script>
Error;
		}
		else {
			$oid = $_SESSION["oid"];
			$date = date("Y-m-d");
			$query = mysql_query("SELECT * FROM oa_items WHERE OAI_OrderAcc_ID = {$oid}");
			$count = mysql_num_rows($query);
			$add = count($select);
			$total = $count + $add;
			if ($total > 24) {
				echo <<<Error
					<script>
						alert("Too many items in inventory.");
						window.location="cust.php";
					</script>
Error;
			}
			else {
				for($i = 0; $i < $add; $i++) {
					mysql_query("INSERT INTO oa_items VALUES({$oid}, {$select[$i]}, '{$date}')");
				}
				echo <<<Success
					<script>
						alert("{$add} item(s) added to inventory. ");
						window.location="cust.php";
					</script>
Success;
			}
		}
	}
	else {
		$oid = $_SESSION["oid"];
		$queryi = mysql_query("SELECT * FROM items ORDER BY Items_ID");
		$queryb = mysql_query("SELECT * FROM items WHERE Item_Cat = 'B' ORDER BY Items_ID");
		$queryc = mysql_query("SELECT * FROM items WHERE Item_Cat = 'C' ORDER BY Items_ID");
		$queryg = mysql_query("SELECT * FROM items WHERE Item_Cat = 'G' ORDER BY Items_ID");
		$queryh = mysql_query("SELECT * FROM items WHERE Item_Cat = 'H' ORDER BY Items_ID");
		$query = mysql_query("SELECT * FROM oa_items WHERE OAI_OrderAcc_ID = {$oid}");
		$count = mysql_num_rows($query);
?>
<?php
	if ($count != 0) {
?>
<h4 style="text-align: center;"><u>Remove Items</u></h4>
<table width="100%" cellpadding="0" cellspacing="0">
	<tr>
		<th>Item Name</th>
		<th>Item Category</th>
		<th>Remove Item?</th>
	</tr>
<?php
	while ($inventory = mysql_fetch_array($queryi)) {
		extract($inventory);
		$checks = mysql_query("SELECT * FROM oa_items WHERE OAI_Items_ID = {$Items_ID} AND OAI_OrderAcc_ID = {$oid}");
		$check = mysql_num_rows($checks);
		if ($check != 0) {
			$category = mysql_query("SELECT * FROM cat WHERE Cat_ID = '{$Item_Cat}'");
			$cat = mysql_fetch_array($category);
			extract($cat);
			echo <<<Inventory
	<tr>
		<td>{$Items_Description}</td>
		<td>{$Cat_Description}</td>
		<td><a href="delete.php?id={$Items_ID}">Remove</a></td>
	</tr>
Inventory;
		}
	}
?>
</table>
<?php
	}
	if ($count < 24) {
?>
<h4 style="text-align: center;"><u>Add Items</u></h4>
<form action="cust.php" method="post">
	<div class="form_settings">
		<h5>Baked Goods</h5>
		<table width="100%" cellpadding="0" cellspacing="0">
			<tr>
				<th>Item Name</th>
				<th>Add Item?</th>
			</tr>
<?php
		while ($baked = mysql_fetch_array($queryb)) {
			extract($baked);
			$checks = mysql_query("SELECT * FROM oa_items WHERE OAI_Items_ID = {$Items_ID} AND OAI_OrderAcc_ID = {$oid}");
			$check = mysql_num_rows($checks);
			if ($check == 0) {
				echo <<<Baked
			<tr>
				<td>{$Items_Description}</td>
				<td><input type="checkbox" name="select[]" value={$Items_ID} /></td>
			</tr>
Baked;
			}
		}
?>
		</table>
		<br />
		<h5>Chips</h5>
		<table width="100%" cellpadding="0" cellspacing="0">
			<tr>
				<th>Item Name</th>
				<th>Add Item?</th>
			</tr>
<?php
		while ($chips = mysql_fetch_array($queryc)) {
			extract($chips);
			$checks = mysql_query("SELECT * FROM oa_items WHERE OAI_Items_ID = {$Items_ID} AND OAI_OrderAcc_ID = {$oid}");
			$check = mysql_num_rows($checks);
			if ($check == 0) {
				echo <<<Chips
			<tr>
				<td>{$Items_Description}</td>
				<td><input type="checkbox" name="select[]" value={$Items_ID} /></td>
			</tr>
Chips;
			}
		}
?>
		</table>
		<br />
		<h5>Gum</h5>
		<table width="100%" cellpadding="0" cellspacing="0">
			<tr>
				<th>Item Name</th>
				<th>Add Item?</th>
			</tr>
<?php
		while ($gum = mysql_fetch_array($queryg)) {
			extract($gum);
			$checks = mysql_query("SELECT * FROM oa_items WHERE OAI_Items_ID = {$Items_ID} AND OAI_OrderAcc_ID = {$oid}");
			$check = mysql_num_rows($checks);
			if ($check == 0) {
				echo <<<Gum
			<tr>
				<td>{$Items_Description}</td>
				<td><input type="checkbox" name="select[]" value={$Items_ID} /></td>
			</tr>
Gum;
			}
		}
?>
		</table>
		<br />
		<h5>Healthy Alternatives</h5>
		<table width="100%" cellpadding="0" cellspacing="0">
			<tr>
				<th>Item Name</th>
				<th>Add Item?</th>
			</tr>
<?php
		while ($healthy = mysql_fetch_array($queryh)) {
			extract($healthy);
			$checks = mysql_query("SELECT * FROM oa_items WHERE OAI_Items_ID = {$Items_ID} AND OAI_OrderAcc_ID = {$oid}");
			$check = mysql_num_rows($checks);
			if ($check == 0) {
				echo <<<Healthy
			<tr>
				<td>{$Items_Description}</td>
				<td><input type="checkbox" name="select[]" value={$Items_ID} /></td>
			</tr>
Healthy;
			}
		}
?>
		</table>
		<br />
<?php
		
		$query = mysql_query("SELECT * FROM oa_items WHERE OAI_OrderAcc_ID = {$oid}");
		$count = mysql_num_rows($query);
		$left = 24 - $count;
?>
		<p style="text-align: center;">
			Space Remaining: <?php echo $left; ?><br />
			<input type="submit" value="Add" name="submit" class="submit" style="margin-left: 0px;" />
		</p>
	</div>
</form>
<?php
	}
	}
	foot();
?>